package com.y17376065_yzh_FinalTest.rdtt.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.y17376065_yzh_FinalTest.base.base.BaseActivity;
import com.y17376065_yzh_FinalTest.base.base.BasePresenter;
import com.y17376065_yzh_FinalTest.rdtt.R;
import com.y17376065_yzh_FinalTest.rdtt.config.Config;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 功能：新闻收藏页面
 */
public class SplashActivity extends BaseActivity
{

    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getResourceId()
    {
        return R.layout.activity_splash;
    }

    @Override
    public void initView(Bundle savedInstanceState)
    {
        super.initView(savedInstanceState);
        //关闭导航栏
        final View decorView = getWindow().getDecorView();
        final int uiOption = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOption);
        RxPermissions permiss = new RxPermissions(this);
        permiss.request(Manifest.permission.READ_PHONE_STATE)
                .subscribe(new Consumer<Boolean>()
                {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception
                    {
                        delay();
                    }
                });
    }


    /*延迟启动App*/
    private void delay()
    {
        Observable.timer(Config.LONG_SPLASH_DELAY, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<Long>()
                {
                    @Override
                    public void accept(Long aLong) throws Exception
                    {
                        startActivity(new Intent(SplashActivity.this, LogActivity.class));
                        doFinish();
                    }
                });
    }
}
